#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homePage.h"
#include "./../../inc/login.h"
#include "./../../inc/setting.h"

static int clicked              = 0;
static uint32_t last_click_time = 0;
static lv_obj_t * username_ta   = NULL;
static lv_obj_t * password_ta   = NULL;
static lv_obj_t * username_ta1  = NULL;
static lv_obj_t * password_ta1  = NULL;

const char * username = NULL;
const char * password = NULL;

static void login_btn_event(lv_event_t * e);
static void close_login_event(lv_event_t * e);
static void register_btn_event(lv_event_t * e);
static void close_register_event(lv_event_t * e);
static void confirm_btn_event(lv_event_t * e);
static bool is_valid_password(const char * password);

void login_init(lv_obj_t * parent)
{
    // 创建半透明背景层
    lv_obj_t * overlay = lv_obj_create(parent);
    lv_obj_set_size(overlay, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_bg_color(overlay, lv_color_black(), 0);
    lv_obj_set_style_bg_opa(overlay, LV_OPA_50, 0);
    lv_obj_set_style_border_width(overlay, 0, 0);
    lv_obj_center(overlay);
    lv_obj_clear_flag(overlay, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    // 创建登录弹窗
    lv_obj_t * login = lv_obj_create(overlay);
    lv_obj_set_size(login, 400, 300);
    lv_obj_set_style_radius(login, 12, 0);
    lv_obj_set_style_bg_color(login, lv_color_white(), 0);
    lv_obj_set_style_bg_opa(login, LV_OPA_100, 0);
    lv_obj_set_style_border_width(login, 2, 0);
    lv_obj_set_style_border_color(login, lv_color_hex(0xE0E0E0), 0);
    lv_obj_set_style_shadow_width(login, 20, 0);
    lv_obj_set_style_shadow_opa(login, LV_OPA_30, 0);
    lv_obj_center(login);

    // 添加标题
    lv_obj_t * title = lv_label_create(login);
    lv_label_set_text(title, "登录");
    lv_obj_set_style_text_color(title, lv_color_make(0, 0, 0), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);

    // 用户名输入框容器
    lv_obj_t * username_cont = lv_obj_create(login);
    lv_obj_set_size(username_cont, 300, 50);
    lv_obj_set_style_bg_opa(username_cont, LV_OPA_0, 0);
    lv_obj_set_style_border_width(username_cont, 0, 0);
    lv_obj_set_style_pad_all(username_cont, 0, 0);
    lv_obj_align(username_cont, LV_ALIGN_TOP_MID, 0, 60);
    lv_obj_clear_flag(username_cont, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    // 创建图标
    lv_obj_t * username_icon = lv_img_create(username_cont);
    lv_img_set_src(username_icon, "S:res/ui/username.png");
    lv_obj_set_size(username_icon, 48, 48);
    lv_obj_set_pos(username_icon, 2, 2);

    // 创建输入框
    username_ta = lv_textarea_create(username_cont);
    lv_obj_set_size(username_ta, 250, 48);                                      // 宽度调整
    lv_obj_align_to(username_ta, username_icon, LV_ALIGN_OUT_RIGHT_MID, 10, 0); // 在图标右边10px
    lv_obj_set_style_radius(username_ta, 6, 0);
    lv_obj_set_style_border_width(username_ta, 1, 0);
    lv_obj_set_style_border_color(username_ta, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_pos(username_ta, 50, 0);
    lv_textarea_set_max_length(username_ta, 20); // 最大长度

    // 密码输入框容器
    lv_obj_t * password_cont = lv_obj_create(login);
    lv_obj_set_size(password_cont, 300, 50);
    lv_obj_set_style_bg_opa(password_cont, LV_OPA_0, 0);
    lv_obj_set_style_border_width(password_cont, 0, 0);
    lv_obj_set_style_pad_all(password_cont, 0, 0);
    lv_obj_align(password_cont, LV_ALIGN_TOP_MID, 0, 130);
    lv_obj_clear_flag(password_cont, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    lv_obj_t * password_icon = lv_img_create(password_cont);
    lv_img_set_src(password_icon, "S:res/ui/password.png");
    lv_obj_set_size(password_icon, 48, 48);
    lv_obj_set_pos(password_icon, 2, 2);

    password_ta = lv_textarea_create(password_cont);
    lv_obj_set_size(password_ta, 250, 48);
    lv_obj_align_to(password_ta, password_icon, LV_ALIGN_OUT_RIGHT_MID, 10, 0); // 在图标右边10px
    lv_obj_set_style_radius(password_ta, 6, 0);
    lv_obj_set_style_border_width(password_ta, 1, 0);
    lv_obj_set_style_border_color(password_ta, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_pos(password_ta, 50, 0);
    lv_textarea_set_max_length(password_ta, 20);

    // 登录按钮
    lv_obj_t * login_btn = lv_btn_create(login);
    lv_obj_set_size(login_btn, 80, 45);

    lv_obj_align(login_btn, LV_ALIGN_BOTTOM_MID, 100, -10);
    lv_obj_set_style_radius(login_btn, 8, 0);
    lv_obj_set_style_bg_color(login_btn, lv_color_hex(0x2196F3), 0);

    // 登录按钮文本​
    lv_obj_t * login_label = lv_label_create(login_btn);
    lv_label_set_text(login_label, "登录");
    lv_obj_set_style_text_color(login_label, lv_color_white(), 0);
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(login_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_center(login_label);

    // 注册按钮
    lv_obj_t * register_btn = lv_btn_create(login);
    lv_obj_set_size(register_btn, 80, 45);

    lv_obj_align(register_btn, LV_ALIGN_BOTTOM_MID, -100, -10);
    lv_obj_set_style_radius(register_btn, 8, 0);
    lv_obj_set_style_bg_color(register_btn, lv_color_hex(0x4CAF50), 0);

    // 注册按钮文本
    lv_obj_t * register_label = lv_label_create(register_btn);
    lv_label_set_text(register_label, "注册");
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(register_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(register_label, lv_color_white(), 0);
    lv_obj_center(register_label);

    // 关闭按钮
    lv_obj_t * close_btn = lv_img_create(login);
    lv_img_set_src(close_btn, "S:res/ui/close.png");
    lv_obj_set_size(close_btn, 32, 32);
    lv_obj_set_pos(close_btn, 2, 2);
    lv_obj_align(close_btn, LV_ALIGN_TOP_RIGHT, -2, 2);
    lv_obj_add_flag(close_btn, LV_OBJ_FLAG_CLICKABLE); // 可点击

    // 添加键盘
    kb = lv_keyboard_create(overlay);
    lv_obj_set_size(kb, LV_HOR_RES, LV_VER_RES / 2);
    lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 20);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 初始隐藏

    // 为文本框添加事件回调
    lv_obj_add_event_cb(username_ta, ta_event_cb, LV_EVENT_ALL, kb);
    lv_obj_add_event_cb(password_ta, ta_event_cb, LV_EVENT_ALL, kb);

    // 添加事件
    lv_obj_add_event_cb(login_btn, login_btn_event, LV_EVENT_CLICKED, overlay);
    lv_obj_add_event_cb(close_btn, close_register_event, LV_EVENT_CLICKED, overlay);
    lv_obj_add_event_cb(register_btn, register_btn_event, LV_EVENT_CLICKED, overlay);
    lv_obj_add_event_cb(overlay, close_login_event, LV_EVENT_CLICKED, overlay);
}

void register_init(lv_obj_t * parent)
{
    // 创建半透明背景层
    lv_obj_t * overlay = lv_obj_create(parent);
    lv_obj_set_size(overlay, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_bg_color(overlay, lv_color_black(), 0);
    lv_obj_set_style_bg_opa(overlay, LV_OPA_50, 0);
    lv_obj_set_style_border_width(overlay, 0, 0);
    lv_obj_center(overlay);
    lv_obj_clear_flag(overlay, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    // 创建登录弹窗
    lv_obj_t * login = lv_obj_create(overlay);
    lv_obj_set_size(login, 400, 300);
    lv_obj_set_style_radius(login, 12, 0);
    lv_obj_set_style_bg_color(login, lv_color_white(), 0);
    lv_obj_set_style_bg_opa(login, LV_OPA_100, 0);
    lv_obj_set_style_border_width(login, 2, 0);
    lv_obj_set_style_border_color(login, lv_color_hex(0xE0E0E0), 0);
    lv_obj_set_style_shadow_width(login, 20, 0);
    lv_obj_set_style_shadow_opa(login, LV_OPA_30, 0);
    lv_obj_clear_flag(login, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动
    lv_obj_center(login);

    // 添加标题
    lv_obj_t * title = lv_label_create(login);
    lv_label_set_text(title, "注册");
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(title, lv_color_hex(0x333333), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
    lv_obj_clear_flag(title, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    // 用户名输入框容器
    lv_obj_t * username_cont = lv_obj_create(login);
    lv_obj_set_size(username_cont, 300, 50);
    lv_obj_set_style_bg_opa(username_cont, LV_OPA_0, 0);
    lv_obj_set_style_border_width(username_cont, 0, 0);
    lv_obj_set_style_pad_all(username_cont, 0, 0);
    lv_obj_align(username_cont, LV_ALIGN_TOP_MID, 0, 60);
    lv_obj_clear_flag(username_cont, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    // 创建图标
    lv_obj_t * username_icon = lv_img_create(username_cont);
    lv_img_set_src(username_icon, "S:res/ui/username.png");
    lv_obj_set_size(username_icon, 48, 48);
    lv_obj_set_pos(username_icon, 2, 2);

    // 创建输入框
    username_ta1 = lv_textarea_create(username_cont);
    lv_obj_set_size(username_ta1, 250, 48);                                      // 宽度调整
    lv_obj_align_to(username_ta1, username_icon, LV_ALIGN_OUT_RIGHT_MID, 10, 0); // 在图标右边10px
    lv_obj_set_style_radius(username_ta1, 6, 0);
    lv_obj_set_style_border_width(username_ta1, 1, 0);
    lv_obj_set_style_border_color(username_ta1, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_pos(username_ta1, 50, 0);
    lv_textarea_set_max_length(username_ta1, 20); // 最大长度

    // 密码输入框容器
    lv_obj_t * password_cont = lv_obj_create(login);
    lv_obj_set_size(password_cont, 300, 50);
    lv_obj_set_style_bg_opa(password_cont, LV_OPA_0, 0);
    lv_obj_set_style_border_width(password_cont, 0, 0);
    lv_obj_set_style_pad_all(password_cont, 0, 0);
    lv_obj_align(password_cont, LV_ALIGN_TOP_MID, 0, 130);
    lv_obj_clear_flag(password_cont, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    lv_obj_t * password_icon = lv_img_create(password_cont);
    lv_img_set_src(password_icon, "S:res/ui/password.png");
    lv_obj_set_size(password_icon, 48, 48);
    lv_obj_set_pos(password_icon, 2, 2);

    password_ta1 = lv_textarea_create(password_cont);
    lv_obj_set_size(password_ta1, 250, 48);
    lv_obj_align_to(password_ta1, password_icon, LV_ALIGN_OUT_RIGHT_MID, 10, 0); // 在图标右边10px
    lv_obj_set_style_radius(password_ta1, 6, 0);
    lv_obj_set_style_border_width(password_ta1, 1, 0);
    lv_obj_set_style_border_color(password_ta1, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_pos(password_ta1, 50, 0);
    lv_textarea_set_max_length(password_ta1, 20); // 最大长度

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(login);
    lv_obj_set_size(back_btn, 80, 45);

    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_MID, -100, -10);
    lv_obj_set_style_radius(back_btn, 8, 0);
    lv_obj_set_style_bg_color(back_btn, lv_color_hex(0x9E9E9E), 0);

    // 返回按钮文本
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回");
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(back_label, lv_color_white(), 0);
    lv_obj_center(back_label);

    // 确认按钮
    lv_obj_t * confirm_btn = lv_btn_create(login);
    lv_obj_set_size(confirm_btn, 80, 45);

    lv_obj_align(confirm_btn, LV_ALIGN_BOTTOM_MID, 100, -10);
    lv_obj_set_style_radius(confirm_btn, 8, 0);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_hex(0x2196F3), 0);

    // 确认按钮文本
    lv_obj_t * confirm_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_label, "确认");
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(confirm_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(confirm_label, lv_color_white(), 0);
    lv_obj_center(confirm_label);

    // 添加键盘
    kb = lv_keyboard_create(overlay);
    lv_obj_set_size(kb, LV_HOR_RES, LV_VER_RES / 2);
    lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 初始隐藏

    // 为文本框添加事件回调
    lv_obj_add_event_cb(username_ta1, ta_event_cb, LV_EVENT_ALL, kb);
    lv_obj_add_event_cb(password_ta1, ta_event_cb, LV_EVENT_ALL, kb);

    // 添加事件
    lv_obj_add_event_cb(confirm_btn, confirm_btn_event, LV_EVENT_CLICKED, overlay);
    lv_obj_add_event_cb(overlay, close_login_event, LV_EVENT_CLICKED, overlay);
    lv_obj_add_event_cb(back_btn, close_register_event, LV_EVENT_CLICKED, overlay);
}

// 登录按钮事件
static void login_btn_event(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED) {
        // 直接使用全局变量或通过其他方式获取文本框对象
        username = lv_textarea_get_text(username_ta);
        password = lv_textarea_get_text(password_ta);

        if(strlen(username) != 0 || strlen(password) != 0) {
            int status = login(user_headNode, username, password);
            if(status == 0) {
                lv_obj_t * overlay = (lv_obj_t *)lv_event_get_user_data(e);
                if(overlay) {
                    lv_obj_del(overlay);
                }
                show_user_card(setting_card);
                show_home_avatar(home_page);
            } else if(status == -1) {
                show_error_window("密码错误!", 1);
            } else {
                show_error_window("用户不存在!", 1);
            }
            username = NULL;
            password = NULL;
        } else {
            show_error_window("账号密码不为空!", 1);
        }
    }
}

// 关闭登录窗口事件
static void close_login_event(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        uint32_t current_time = lv_tick_get();
        lv_obj_t * overlay    = (lv_obj_t *)lv_event_get_user_data(e);

        // 检查时间间隔
        if(current_time - last_click_time > 300) {
            clicked = 0; // 超过300ms，重置计数
        }

        clicked++;
        last_click_time = current_time;

        // 如果是第二次点击且在时间窗口内，认为是双击
        if(clicked == 2) {

            clicked = 0;

            if(overlay) {
                lv_obj_del(overlay);
            }
        }
    }
}

static void register_btn_event(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * login_scr = lv_event_get_user_data(e);
        register_init(login_scr);
    }
}

static void close_register_event(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * overlay = (lv_obj_t *)lv_event_get_user_data(e);
        lv_obj_del(overlay);
    }
}

static void confirm_btn_event(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        // 直接使用全局变量或通过其他方式获取文本框对象
        const char * username = lv_textarea_get_text(username_ta1);
        const char * password = lv_textarea_get_text(password_ta1);

        if(strlen(username) == 0 || strlen(password) == 0) {
            show_error_window("账号密码不为空!", 1);
        } else if(strlen(username) > 16) {
            show_error_window("账号长度不能超过16位!", 2);
        }
        // 密码格式验证
        else if(!is_valid_password(password)) {
            show_error_window("密码至少8位,需包含大小写字母和数字!", 2);
        } else {
            int status = register_user(user_headNode, username, password);
            if(status == 0) {
                lv_obj_t * overlay = (lv_obj_t *)lv_event_get_user_data(e);
                lv_obj_del(overlay);
            } else {
                show_error_window("账号已存在!", 1);
            }
        }
    }
}

// 密码验证函数 至少8位，包含大小写字母和数字
static bool is_valid_password(const char * password)
{
    if(password == NULL) return false;

    int len = strlen(password);
    // 密码长度至少8位
    if(len < 8) return false;

    bool has_upper = false;
    bool has_lower = false;
    bool has_digit = false;

    for(int i = 0; i < len; i++) {
        if(isupper(password[i])) {
            has_upper = true;
        } else if(islower(password[i])) {
            has_lower = true;
        } else if(isdigit(password[i])) {
            has_digit = true;
        }
        // else if(strchr("!@#$%^&*()", password[i])) { ... }
    }

    return has_upper && has_lower && has_digit;
}